package com.javalong.affairs.base.ui;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.TextView;

import com.javalong.affairs.R;


/**
 * Created by 杨观回 on 2015/12/29 0029.
 */
public class QSToolbar extends Toolbar {
    private TextView tv_title;

    public QSToolbar(Context context) {
        super(context);
        init();
    }

    public QSToolbar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TWToolbar);
        for (int i = 0; i < typedArray.getIndexCount(); i++) {
            int attr = typedArray.getIndex(i);
            int resId;
            switch (attr) {
                case R.styleable.TWToolbar_tb_titleTextColor:
                    resId = typedArray.getResourceId(R.styleable.TWToolbar_tb_titleTextColor, 0);
                    if (resId != 0) {
                        tv_title.setTextColor(getResources().getColor(resId));
                    } else {
                        tv_title.setTextColor(typedArray.getColor(R.styleable.TWToolbar_tb_titleTextColor, 0));
                    }
                    break;
                case R.styleable.TWToolbar_tb_backgroud:
                    resId = typedArray.getResourceId(R.styleable.TWToolbar_tb_backgroud, 0);
                    if (resId != 0) {
                        setBackgroundResource(resId);
                    } else {
                        int color = typedArray.getColor(R.styleable.TWToolbar_tb_backgroud, 0);
                        if (color == 0) {
                            continue;
                        }
                        setBackgroundColor(color);
                    }
                    break;
            }
        }
        typedArray.recycle();
    }

    private void init() {
        setBackgroundResource(R.color.bg_title);
    }

    @Override
    public void setTitle(CharSequence title) {
        //设置title时，默认屏蔽掉
        super.setTitle(null);
        setCenterTitle(title);
    }

    @Override
    public CharSequence getTitle() {
        if (tv_title == null) {
            return null;
        }
        return tv_title.getText().toString();
    }

    public void setCenterTitle(CharSequence title) {
        if (tv_title == null) {
            tv_title = new TextView(getContext());
            Toolbar.LayoutParams ll_param = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            ll_param.gravity = Gravity.CENTER;
            tv_title.setGravity(Gravity.CENTER);
            addView(tv_title, ll_param);
            tv_title.setTextColor(getResources().getColor(R.color.text_title));
            tv_title.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.text_size_title));
        }
        tv_title.setText(title);
    }
}
